#!/usr/bin/python
# -*- coding: utf8

__author__ = 'daddy-bear'


def __oneLoop(graph):
    graphStateStr = str(graph)
    if not graphStateStr.startswith("e11|e|:"):
        return None
    vertices = list(graph.vertices() - set([graph.externalVertex]))
    edges = filter(lambda e: graph.externalVertex not in e.nodes, graph.edges(vertices[0]))

    import phi4.lambda_number

    alpha = phi4.lambda_number.from_rainbow(edges[0])
    beta = phi4.lambda_number.from_rainbow(edges[1])
    not_empty = None
    if graphStateStr[-2] != ":":
        not_empty = filter(lambda e: e.arrow is not None and not e.arrow.is_null(), edges)
    if not_empty is not None and len(not_empty):
        not_empty_numerator_edge = not_empty[0]
        copy = list(edges)
        copy.remove(not_empty_numerator_edge)
        other_edge = copy[0]
        if other_edge.arrow.is_null():
            sign = "" if not_empty_numerator_edge.arrow.is_left() else "-"
            if not_empty_numerator_edge != edges[0]:
                t = alpha
                alpha = beta
                beta = t
            return (sign + "G1(" + str(alpha) + ", " + str(beta) + ")",
                    phi4.lambda_number.to_rainbow((alpha + beta - phi4.lambda_number.lambda_number((1, 1)))))
        else:
            sign = "" if not_empty_numerator_edge.arrow == other_edge.arrow else "-"
            return (sign + "G2(" + str(alpha) + ", " + str(beta) + ")",
                    phi4.lambda_number.to_rainbow((alpha + beta - phi4.lambda_number.lambda_number((2, 1)))))

    return ("G(" + str(alpha) + ", " + str(beta) + ")",
            phi4.lambda_number.to_rainbow((alpha + beta - phi4.lambda_number.lambda_number((1, 1)))))